//
//  MenuScene.cpp
//  BattleRecipes
//
//  Created by Truong Hoang Son on 3/20/14.
//
//

#include "MenuScene.h"
#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "Header.h"
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* MenuScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    MenuScene *layer = MenuScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MenuScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    /* Add DotA2 Background */
    CCSprite* dota2Icon = CCSprite::create("background-main.png");
    dota2Icon->setScaleX(SIZE.height/dota2Icon->getContentSize().width);
    dota2Icon->setScaleY(SIZE.height/dota2Icon->getContentSize().height);
    dota2Icon->setAnchorPoint(ccp(1, 0));
    dota2Icon->setPosition(ccp(SIZE.width, 0));
    dota2Icon->setTag(kTAG_IMAGE_MAIN);
    addChild(dota2Icon);
    
    CCSprite* dota2BG = CCSprite::create("background2.png");
    dota2BG->setScaleX((SIZE.width - dota2Icon->getScaleX()*dota2Icon->getContentSize().width)/dota2BG->getContentSize().width);
    dota2BG->setScaleY(SIZE.height/(dota2BG->getContentSize().height));
    dota2BG->setAnchorPoint(ccp(0, 0));
    dota2BG->setPosition(ccp(0, 0));
    addChild(dota2BG);
    
    /* Menu Play Game */
    CCMenuItemImage* itemContinue = CCMenuItemImage::create("continue-button.png", "continue-hover.png", "continue-button-dis.png", this, NULL);
    itemContinue->setEnabled(false);
    
    CCMenuItemImage* itemNewGame = CCMenuItemImage::create("newgame-button.png", "newgame-hover.png",  this,menu_selector(MenuScene::playNewGame));
    
    CCMenu* menuPlay = CCMenu::create(itemContinue,itemNewGame,NULL);
    menuPlay->alignItemsVerticallyWithPadding(itemNewGame->getContentSize().height/2);
    menuPlay->setAnchorPoint(ccp(0.5, 1));
    menuPlay->setPosition(ccp(dota2BG->getScaleX()*dota2BG->getContentSize().width/2, SIZE.height - itemNewGame->getContentSize().height*2));
    
    addChild(menuPlay, 1);
    
    /* Menu Settings */
    CCMenuItemImage* itemRanking = CCMenuItemImage::create("rank-button.png", "rank-hover.png", this, NULL);
    CCMenuItemImage* itemHowToPlay = CCMenuItemImage::create("howtoplay-button.png", "howtoplay-hover.png", this, menu_selector(MenuScene::howToPlay));
    CCMenuItemImage* itemAbout = CCMenuItemImage::create("about-button.png", "about-hover.png", this, menu_selector(MenuScene::showAbout));
    
    CCMenu* menuSettings = CCMenu::create(itemRanking,itemHowToPlay,itemAbout,NULL);
    menuSettings->alignItemsVerticallyWithPadding(itemAbout->getContentSize().height/2);
    menuSettings->setAnchorPoint(ccp(0.5, 0));
    menuSettings->setPosition(ccp(dota2BG->getScaleX()*dota2BG->getContentSize().width/2, itemAbout->getContentSize().height*3));
    
    addChild(menuSettings, 1);
    return true;
}

void MenuScene::showAbout(){
    CCTextureCache::sharedTextureCache()->removeAllTextures();
    CCSpriteFrameCache::sharedSpriteFrameCache()->purgeSharedSpriteFrameCache();
    
    CCSprite* background = (CCSprite*)getChildByTag(kTAG_IMAGE_MAIN);
    CCTexture2D * txt = CCTextureCache::sharedTextureCache()->addImage("about-bg.png");
    CCSpriteFrame* sf = CCSpriteFrame::createWithTexture(txt, CCRect(0, 0, txt->getContentSize().width, txt->getContentSize().height));
    background->setDisplayFrame(sf);
}
void MenuScene::howToPlay(){
    CCTextureCache::sharedTextureCache()->removeAllTextures();
    CCSpriteFrameCache::sharedSpriteFrameCache()->purgeSharedSpriteFrameCache();
    
    CCSprite* background = (CCSprite*)getChildByTag(kTAG_IMAGE_MAIN);
    CCTexture2D * txt = CCTextureCache::sharedTextureCache()->addImage("howtoplay-bg.png");
    CCSpriteFrame* sf = CCSpriteFrame::createWithTexture(txt, CCRect(0, 0, txt->getContentSize().width, txt->getContentSize().height));
    background->setDisplayFrame(sf);
}
void MenuScene::playNewGame(){
    removeAllChildrenWithCleanup(true);
    CCTextureCache::sharedTextureCache()->removeAllTextures();
    CCSpriteFrameCache::sharedSpriteFrameCache()->purgeSharedSpriteFrameCache();
    
    CCDirector::sharedDirector()->replaceScene(GameScene::scene());
}